/**
 * Created by zhengsenwen on 2017/9/22.
 */

drag();

function drag() {

    var mouseDown = false;  //移动标记
    var X, Y; //鼠标离控件左上角的相对位置
    var x, initx; //控件左上角到屏幕左上角的相对位置

    $(".takeaway_ul").mousedown(function (e) {
        mouseDown = true;
        X = e.pageX - parseInt($(".takeaway_ul").css("margin-left"));
        //initx = $("#takeaway_div").width() * 0.05;
        $("#start").text(e.pageX + "    -   " + $(".takeaway_ul").css("margin-left") + "  =  " + X);
    });
    $(".takeaway_ul").mousemove(function (e) {

        function  move(){
            if (mouseDown) {
                var diff = $(".takeaway_ul").width() - $("#takeaway_box").width();
                x = e.pageX - X;
                $("#end").text(diff + "     " + x);

                if ((-diff) < x && x < 0) {
                    $(".takeaway_ul").css("margin-left", x);
                } else if (x < (-diff)) {
                    $(".takeaway_ul").css("margin-left", -diff);
                } else if (x > 0) {
                    $(".takeaway_ul").css("margin-left", 0);
                } else {
                }
            } else {}
        }

        var count = $("#takeaway_box").width()  /  $(".takeaway_ul li").width();
        var number = Math.ceil(count);
        if($(".takeaway_ul li").length >= number){
            alert($(".takeaway_ul li").length+"   "+number);
            move();
        }else{}

    });
    $(".takeaway_ul").mouseup(function () {
        mouseDown = false;
    })
}